<?php

class MessageController extends OController {

    public $defaultAction='admin';
    public $homeUrl;
    public $cancelUrl;
    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;

    public function init(){
        parent::init();
        $this->homeUrl=$this->createUrl($this->id.'/'.$this->defaultAction);
    }
    
    public function actionAdmin() {
        $model = new Message('adminSearch');
        $model->unsetAttributes();
        $params = Yii::app()->request->getParam(get_class($model));
        if ($params)
            $model->attributes = $params;

        $this->render('admin', array(
            'model' => $model,
            'dataProvider'=>$model->adminSearch(),
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
            ),
        ));
    }

    public function actionView($id) {
        $this->render('view', array(
            'data' => $this->loadModel($id),
        ));
    }

    public function actionDelete($id) {
        $model = $this->loadModel($id);
        $model->scenario='adminDelete';
        if ($model->deleteRecord()) {
            $this->setFlash('success', Lang::t('user.delete.flash.success'));
        } else {
            $this->setFlash('fail', Lang::t('user.delete.flash.fail'));
            //if($this->debugMode) print_r($model->getErrors());
        }
        $this->redirect(array('admin'));
    }

//end function

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    public function loadModel($id = null) {
        if ($this->_model === null) {
            if (empty($id)) HttpStatus::code(404);
            
            $this->_model = Message::model()->accessible()->findByPk((int) $id);
            if (empty($this->_model))
                HttpStatus::code(404);
        }
        return $this->_model;
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}//end class